package test.tasks

import scalab.core.component.Task
import scalab.core.Cache
import scalab.caches.ccp.AccurateConservativeCCP
import scalab.caches.cdp.TimestampCDP
import scalab.caches.cep.LRUCEP
import scalab.core.dependencies.Dependency

object DummyFiler{
  val CREATE_FILES: Int = 0

	def apply(): DummyFiler = new DummyFiler{}
}

trait DummyFiler extends Task{

  import java.io.File
  import java.lang.{Double, Math}
	val myCache: Cache = new AccurateConservativeCCP with TimestampCDP with LRUCEP{
		val name: String = "myCache"
	}
	setCache(myCache)
	myCache.setEvictedSize(512)
	myCache.setThresholdSize(102400)
	
	override val name = "DummyFiler"
  def createEmptyFile(dep: Dependency): Boolean = {

		try{
			
			dep.getParentFile.mkdirs
			val created = dep.createNewFile()
			this transmit dep
    	if(!created){
				info("File " + dep.canonicalPath + " already exists")
			}
			true
		}
		catch{
			case e: Exception => 
				error("There was an error while creating file " + dep.canonicalPath + ":\n" + e.getMessage)
				false
		}
  }

  def randomContent(path: String): String = {
    (new Double(Math.random())).toString()
  }

	import scalab.core.Resource

  def execute(destinationDir: String): Boolean = {
		import scalab.core.dependencies.HardDependency
		import java.io.File
		/*val lst = in.outOfDateDeps.toList
		val a = lst(0)
		val b = lst(1)
		require(a,b)
		warning(a.canonicalPath + " requires " + b.canonicalPath)
		val rel = filesRelations.get(a)
		rel match{
			case Some(r) =>
				warning("new rel: " + r.toString)
			case None =>
		}*/
		in.outOfDateSources forall {d => 
			val p = HardDependency(destinationDir + d.getPath)
			require(p,d)
			//warning(p.canonicalPath + " requires " + d.canonicalPath)
			createEmptyFile(p)
		}
		
  }

	val in = mandatoryIn

	def instanceId: String = ""
}
